// 新建话题、删除话题、修改话题、查看话题列表
const express = require('express')
const Topic = require('../models/topic')

const router = express.Router()

router.get('/topics/edit', (req, res) => {
    res.render('topic/edit.html', {
        user: req.session.user
    })
})

router.post('/topics/edit', (req, res, next) => {
    new Topic(req.body).save((err, topic) => {
        if(err){
            console.log(err)
            return next(err)
        }
        res.status(200).json({
            err_code: 0,
            msg: 'ok'
        })
    })
})

router.get('/topics/show', (req, res, next) => {
    console.log(req.query)
    const { topicId } = req.query
    Topic.findOne({
        topicId
    }, (err, data) => {
        if(err){
            return next(err)
        }
        console.log(data)
        res.render('topic/show.html', {
            user: req.session.user,
            topic: data
        })
    })
})

module.exports = router